<?php
	class RatingSystemHelper extends AppHelper {
	var $helpers = array('Ajax');

    // list of 'hints' to display for rating
	var $_hints = array('Suck', 'Soso', 'OK', 'Pretty Good', 'One Of The Best');

    function ratingBar($rating_info = null, $text = false, $show_hints = false) {
		if (!$rating_info)
			return;
		
    	$total_width = $rating_info['unit_width'] * $rating_info['units'];
		$current_width = $rating_info['average'] * $rating_info['unit_width'];
		$average_text = $rating_info['average']. '/' . @number_format($rating_info['units'], 2);
		$url = $rating_info['url'];	
			
    	echo($this->Ajax->div('ratingblock'));
		echo("\n\t" . '<div id="unit_long' . $rating_info['id'] . '">');
		echo("\n\t\t" . '<ul id="unit_ul' . $rating_info['id'] . '" class="unit-rating" style="width:' . $total_width. 'px;">');
		echo("\n\t\t\t" . '<li class="current-rating" style="width:' . $current_width . 'px;">');
		echo($average_text . '</li>');
		
		if ($rating_info['login']) {
			// user is logged in
			if (!$rating_info['owner']) {
				// user is not the owner of story
				if (!$rating_info['rated']) {
					// user has not rate the story
					for ($i = 1; $i <= 5; $i++){
						$ajax_options = array('update'=> 'ratingblock', 'class'=> "r$i-unit rater");
						$rate_url = $url . $rating_info['id'] . '/' . $i;
				
						if ($show_hints) {
							$hint_index = $i - 1;
							$ajax_options['title'] = $this->_hints[$hint_index];
						}
				
						echo("\n\t\t\t" . '<li>');
						echo($this->Ajax->link($i, $rate_url, $ajax_options));
						echo('</span></li>');
					}
					
					$i = 0;
				}
				else {
					// user has rated the story
					
				}
			}
			else {
				// user is the owner of the story
				
			}
		}
		else {
			// user is not logged in

		}

		echo("\n\t\t</ul>");
		echo("\n\t</div>");
		
		$num_ratings_text = $rating_info['num_ratings'];
		if ($rating_info['num_ratings'] < 2)
			$num_ratings_text .= ' rating';
		else
			$num_ratings_text .= ' ratings';	
		
		echo("\n\t" . '<div id="ratingtxt">');
		if ($text) {
			echo("\n\t\t" . '<span id="ratingValueText">' . $average_text . '</span>');
		}
		echo("\n\t\t" . '<span id="numRatingsText">' . $num_ratings_text . '</span>');
		echo("\n\t</div>\n");
		echo($this->Ajax->divEnd('ratingblock'));
    }//ratingBar
    
    function giveRating($rating_info = null, $text = false) {
    	$total_width = $rating_info['unit_width'] * $rating_info['units'];
        $current_width = $rating_info['average'] * $rating_info['unit_width'];
        $average_text = $rating_info['average']. '/' . @number_format($rating_info['units'], 2);
    	
        echo("\n\t" . '<div id="unit_long' . $rating_info['id'] . '">');
		echo("\n\t\t" . '<ul id="unit_ul' . $rating_info['id'] . '" class="unit-rating" style="width:' . $total_width. 'px;">');
		echo("\n\t\t\t" . '<li class="current-rating" style="width:' . $current_width . 'px;">');
		echo($average_text . '</li>');
		
		echo("\n\t\t</ul>");
		echo("\n\t</div>");
		
		$num_ratings_text = $rating_info['num_ratings'];
    	if ($rating_info['num_ratings'] < 2) {
			$num_ratings_text .= ' rating';
		}
		else {
			$num_ratings_text .= ' ratings';
		}
		
		echo("\n\t" . '<div id="ratingtxt">');
    	if ($text) {
			echo("\n\t\t" . '<span id="ratingValueText">' . $average_text . '</span>');
		}
		echo("\n\t\t" . '<span id="numRatingsText">' . $num_ratings_text . '</span>');
		echo("\n\t</div>\n");
    }
}//ratingSystemHelper
?>
